//
//  OrderCountView.m
//  WorkNest
//
//  Created by qbm360 on 15/7/15.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "OrderCountView.h"

@interface OrderCountView () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *style;
@property (weak, nonatomic) IBOutlet UITextField *count;
@property (strong, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UIView *countView;
@property (weak, nonatomic) IBOutlet UILabel *styleCount; // 数量
@property (assign, nonatomic) BOOL ifFirstView;

@end

@implementation OrderCountView

- (id)initWithFrame:(CGRect)frame ifFirstView:(BOOL)ifFirstView
{
    self = [super initWithFrame:frame];
    if (self) {
        self.ifFirstView = ifFirstView;
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:
     UIKeyboardWillChangeFrameNotification  object:nil];
    [[NSBundle mainBundle] loadNibNamed:@"OrderCountView" owner:self options:nil];
    [self addSubview:_view];
    _view.frame = self.bounds;
    _count.delegate = self;
//    [self.count addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([self.count.text isEqualToString: @"0"]) {
        self.count.text = @"";
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self.count.text isEqualToString: @""]) {
        self.count.text = @"0";
    } else {
        if (self.countBlock) {
            _countBlock(self.index, self.count.text);
        }
    }
}

//-(void)textFieldDidChange:(id)sender {
//    if (self.countBlock) {
//        _countBlock(self.index, self.count.text);
//    }
//}

- (void)setStyleString:(NSString *)styleString {
    _styleString = styleString;
    self.style.text = _styleString;
    if (self.ifFirstView) {
        self.countView.hidden = YES;
    } else {
        self.countView.hidden = NO;
    }
}

- (void)setCountString:(NSString *)countString {
    _countString = countString;
    self.count.text = _countString;
}

- (IBAction)reduceButton:(id)sender {
    if ([self.count.text integerValue] == 0) {
        return;
    }
    self.count.text = [NSString stringWithFormat:@"%ld",(long)[self.count.text integerValue] - 1];
    if (self.countBlock) {
        _countBlock(self.index, self.count.text);
    }

}

- (IBAction)addButton:(id)sender {
    self.count.text = [NSString stringWithFormat:@"%ld",(long)[self.count.text integerValue] + 1];
    if (self.countBlock) {
        _countBlock(self.index, self.count.text);
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    textField.inputAccessoryView = view;
    view.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SCREEN_WIDTH - 40, 0, 40, 30);
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setTitle:@"完成" forState: UIControlStateNormal];
    [btn addTarget:self action:@selector(checkButton) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [textField reloadInputViews];
    return YES;
}

- (void)checkButton {
    [self.view endEditing:YES];
    if (self.editBlock) {
        _editBlock (NO, 0);
    }
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    if (self.editBlock) {
        _editBlock (YES, kbSize.height);
    }
}

@end
